<!-- 卡券详情界面 -->
<template>
	<view class="coupon-details">
		<view class="top">
			<image :src="baseFormData.cover"></image>
			<view class="top-content">
				<view>
					<text class="title">{{baseFormData.name}}</text>
					<view class="amount">
						<text style="font-size: 28px;">￥{{baseFormData.denomination}}</text>
					</view>
				</view>
				<view class="validity">有效期：{{formatDate(baseFormData.expirationTime)}}</view>
			</view>
		</view>
		<view class="introduce">
			<uni-forms ref="baseForm" :modelValue="baseFormData">
				<uni-forms-item label="介绍" >
					<text class="details">{{baseFormData.description}}</text>
				</uni-forms-item>
			</uni-forms>
		</view>
		<view class="tip">
			最终解释权归望月湖银盆岭街道卫生服务中心所有。
		</view>
		<view class="bottom">
			<button class="bottom-btn" type="default" @click="receive">立即领取</button>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { getCouponDetails, receiveCoupon } from '@/sheep/api/mine.js'
	import { onMounted, getCurrentInstance, ref } from 'vue';
	import dayjs from 'dayjs'
	
	// 日期格式化
	const formatDate = (val) => {
		return dayjs(val).format('YYYY/MM/DD')
	}
	
	onMounted(()=>{
		couponId.value = getCurrentInstance().attrs.id
		loadCouponDetails()
	})
	
	// 券id
	const couponId = ref()
	// 表单数据
	const baseFormData = ref({})
	
	// 加载券详情
	const loadCouponDetails = async() => {
		try{
			const res = await getCouponDetails(couponId.value)
			baseFormData.value = res.data
		}catch(e){
			console.log(e)
		}
	}
	
	// 点击领取券触发
	const receive = async() => {
		try{
			const data = {
				idNumber: '110101199001013232',
				couponSurfaceId: baseFormData.value.id,
			}
			const res = await receiveCoupon(data)
			uni.showToast({
				icon: none,
				title: res.msg,
			});
		}catch(e){
			console.log(e)
		}
	}
</script>

<style lang="scss" scoped>
	.coupon-details {
		position: relative;
		box-sizing: border-box;
		padding: 24px 16px;
		height: 100%;
		background: linear-gradient(180deg, rgba(95, 206, 151, 1) 0%, rgba(255, 255, 255, 1) 100%);
		.top {
			display: flex;
			align-items: center;
			padding: 24px 16px;
			border-radius: 8px;
			background-color: #fff;
			image {
				height: 100px;
				width: 100px;
				border-radius: 8px;
			}
			.top-content {
				margin-left: 12px;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				height: 100px;
					.title {
						font-size: 16px;
						color: #333333;
						font-weight: bold;
					}
					.amount {
						display: flex;
						align-items: center;
						line-height: 40px;
						color: #FD5856;
						font-weight: 500;
					}
					.validity {
						font-size: 13px;
						color: #9D9D9D;
						font-weight: 400;
					}
			}
		}
		.introduce {
			margin-top: 12px;
			padding: 16px 16px 0;
			border-radius: 8px;
			background-color: #fff;
			.details {
				color: #666;
				font-size: 16px;
			}
		}
		.tip {
			font-size: 14px;
			color: #9D9D9D;
			text-align: center;
			margin-top: 12px;
		}
		.bottom {
			width: calc(100vw - 32px);
			position: absolute;
			bottom: 32px;
			left: 16px;
			.bottom-btn {
				background-color: #04B285;
				color: #fff;
				border-radius: 24px;
				font-size: 16px;
			}
		}
	}
	:deep(.uni-forms-item__label) {
		padding: 0;
		align-items: flex-start;
		height: auto;
		font-size: 16px;
	}
</style>